﻿using System;
using System.Net.Mail;
using System.Reflection;
using System.Text;
using eCommerce.DataAccess;

namespace eCommerce.BusinessLogic
{
    public class NewsletterBL
    {
       public Xcms_Subscriber GetSubscriber(string userEmail,string userName)
        {
            if (userEmail == null) throw new ArgumentNullException("userEmail");
            if (userName == null) throw new ArgumentNullException("userName");
            NewsletterDA newsletterDA = new NewsletterDA();
            return newsletterDA.GetSubscriber(userEmail,userName);
        }

       public void SendTechError(string compWebsite, string compSubject, string userEmail, string error1, string error2, MethodBase error3)
       {
           MailAddress from3 = new MailAddress("techError@" + compWebsite);
           MailAddress to3 = new MailAddress("tavis@2beknown.co.uk");
           MailMessage message2 = new MailMessage(from3, to3);
           message2.Subject = compSubject;
           StringBuilder sb2 = new StringBuilder();
           sb2.Append("User Email:" + userEmail + "<br /><br /><br />");
           sb2.Append("Error Message:" + error1 + "<br /><br /><br />");
           sb2.Append("Stack Trace:" + error2 + "<br /><br /><br />");
           sb2.Append("Target Site:" + error3 + "<br />");
           message2.Body = sb2.ToString();
           message2.IsBodyHtml = true;
           SmtpClient smtp2 = new SmtpClient("localhost", 25);
           smtp2.Send(message2);
           
       }

       public void SendEmailReply(string userEmail, string compEmail, string emailSubject, string userName, string compWebsite, string compName)
       {
           // Create Random "Membership" Number 
           // n = new Random().Next(100000, 999999);
           //Console.WriteLine(n.ToString());

           MailAddress from = new MailAddress(compEmail);
           //MailAddress to = new MailAddress(compEmail);
           //Testing Purposes
           MailAddress to = new MailAddress("tavis@2beknown.co.uk");
           MailMessage message = new MailMessage(from, to);
           //message.Bcc.Add("tavis@2beknown.co.uk");
           message.Subject = emailSubject;
           StringBuilder sb = new StringBuilder();
           sb.Append("<html><body style=\"height:auto;margin:0 auto;\"><div style=\"font-family:Arial;font-size:11px;float:left;height:auto;margin:0 auto;color:#3F3F3F;padding:10px;\">");
           sb.Append(" <img src=\"http://" + compWebsite + "/App_Themes/default/graphics/smlogo.jpg\" alt=\"" + compName + "\" /><br /><br />");
           sb.Append("<b><u>" + emailSubject + "</u></b><br /><br />");
           sb.Append("<b>Name:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + userName + "</b><br />");
           sb.Append("<b>Email:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " + userEmail + "</b><br />");
           sb.Append("</div></body></html>");
           message.Body = sb.ToString();
           message.IsBodyHtml = true;
           SmtpClient smtp = new SmtpClient("localhost", 25);
           smtp.Send(message);

           // Stop working here!!


           MailAddress from1 = new MailAddress(compEmail);
           //MailAddress to1 = new MailAddress(userEmail);
           //Testing Purposes
           MailAddress to1 = new MailAddress("tavis@2beknown.co.uk");
           MailMessage message1 = new MailMessage(from1, to1);
           message1.Subject = emailSubject;
           StringBuilder sb1 = new StringBuilder();
           sb1.Append("<html><body style=\"padding:10px;height:auto;margin:0 auto;\"><div style=\"font-family:Arial;font-size:11px;float:left;height:auto;margin:0 auto;color:#3F3F3F;\">");
           sb1.Append(" <img src=\"http://" + compWebsite + "/App_Themes/Default/graphics/smlogo.jpg\" alt=\"" + compName + "\" /><br /><br />");
           sb1.Append("<b>Dear " + userName + "</b><br /><br />");
           sb1.Append("<b>Thank you for subscribing to our newsletter.</b><br /><br />");
           sb1.Append("<b>Regards,</b><br />");
           sb1.Append("<b></b><br />");
           sb1.Append("<b>Managing Director</b><br />");
           sb1.Append("<b><a style=\"color:#3F3F3F;text-decoration:none;\" href='http://" + compWebsite + "'>" + compWebsite + "</a></b><br />");
           sb1.Append("<b><a style=\"color:#3F3F3F;text-decoration:none;\" href='mailto:" + compEmail + "'>" + compEmail + "</a></b><br />");
           sb1.Append("</div></body></html>");
           message1.Body = sb1.ToString();
           message1.IsBodyHtml = true;
           SmtpClient smtp1 = new SmtpClient("localhost", 25);
           smtp1.Send(message1);
       }
    }

   
}
